package com.joysuch.wwyt.openapi.controllers.controllerV1;

import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.exception.ApiCoreException;
import com.joysuch.wwyt.dzhapi.dto.BuildDataDto;
import com.joysuch.wwyt.dzhapi.exception.ApiException;
import com.joysuch.wwyt.dzhapi.service.ApiTokenVerifyService;
import com.joysuch.wwyt.openapi.controllers.controllerV1.req.ApiV1BaseUserAddReq;
import com.joysuch.wwyt.openapi.controllers.controllerV1.req.ApiV1BaseUserReq;
import com.joysuch.wwyt.openapi.controllers.controllerV1.req.ApiV1BaseUserUpdateReq;
import com.joysuch.wwyt.openapi.controllers.controllerV1.req.ApiV1DataReq;
import com.joysuch.wwyt.openapi.controllers.controllerV1.vo.ApiPagePropertyDataVo;
import com.joysuch.wwyt.openapi.controllers.controllerV1.vo.ApiResultData;
import com.joysuch.wwyt.openapi.exception.ApiV1ExceptionCodeMsg;
import com.joysuch.wwyt.openapi.result.ApiV1Result;
import com.joysuch.wwyt.openapi.services.serviceV1.ApiV1BaseUserService;
import com.joysuch.wwyt.openapi.services.serviceV1.ApiV1TokenVerifyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author: JamesZY
 * @date: 2021/8/23
 * @description:人员接口
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/employee")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ApiV1BaseUserController {
    private final ApiV1BaseUserService service;
    private final ApiTokenVerifyService verifyService;

    /**
     * 条件查询所有员工
     */
    @PostMapping("/getAll")
    public ApiV1Result getAllEmployees(@RequestBody @Valid ApiV1BaseUserReq req, HttpServletRequest request) {
        ApiPagePropertyDataVo data = null;
        BuildDataDto buildDataDto;
        try {
            buildDataDto = verifyService.verifyToken(request);
        } catch (ApiException e) {
            log.error("分页查询失败", e);
            return ApiV1Result.error(e.getCode(), e.getMessage());
        }
        data = service.findAll(req, buildDataDto);
        return ApiV1Result.successData(data);
    }


    /**
     * 批量删除员工
     */
    @PostMapping("/batchDelete")
    public ApiV1Result deleteUsers(@RequestBody @Valid ApiV1DataReq<String> req,
                                   HttpServletRequest request) {
        BuildDataDto buildDataDto;
        try {
            buildDataDto = verifyService.verifyToken(request);
        } catch (ApiException e) {
            return ApiV1Result.error(e.getCode(), e.getMessage());
        }
        List<ApiResultData> dataList = service.batchDelete(req.getData(), buildDataDto);
        return ApiV1Result.success(dataList);
    }

    /**
     * 修改员工
     */
    @PostMapping("/update")
    public ApiV1Result updateUser(@RequestBody @Valid ApiV1BaseUserUpdateReq req,
                                  HttpServletRequest request) {
        BuildDataDto buildDataDto;
        try {
            buildDataDto = verifyService.verifyToken(request);
        } catch (ApiException e) {
            return ApiV1Result.error(e.getCode(), e.getMessage());
        }
        try {
            service.update(req, buildDataDto);
        } catch (ApiV1ExceptionCodeMsg e) {
            return ApiV1Result.error(e.getCode(), e.getMessage());
        }
        return ApiV1Result.success();
    }

    /**
     * 批量新增员工
     */
    @PostMapping("/batchAdd")
    public ApiV1Result batchAdd(@RequestBody @Valid ApiV1DataReq<ApiV1BaseUserAddReq> req,
                                HttpServletRequest request) {
        BuildDataDto buildDataDto;
        try {
            buildDataDto = verifyService.verifyToken(request);
        } catch (ApiException e) {
            return ApiV1Result.error(e.getCode(), e.getMessage());
        }
        List<ApiResultData> apiResultData = service.batchAdd(req.getData(), buildDataDto);
        return ApiV1Result.success(apiResultData);
    }
}
